﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace MuaDeal
{
    public partial class XemDSUser : Form
    {
        public XemDSUser()
        {
            InitializeComponent();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtsotien_TextChanged(object sender, EventArgs e)
        {

        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private void XemDSUser_Load(object sender, EventArgs e)
        {
            try
            {
                Datahelper helper = new Datahelper();
                helper.Open();
                string proc = "sp_LayDanhSachUser";
                string procloai = "sp_LayDanhSachLoaiUser";
                string proctinhtrang = "sp_LayDanhSachTinhTrangUser";
                DataTable dt = new DataTable();
                DataTable dtloai = new DataTable();
                DataTable dttinhtrang = new DataTable();
                dt = helper.ExecuteQueryStore(proc);
                dtloai = helper.ExecuteQueryStore(procloai);
                dttinhtrang = helper.ExecuteQueryStore(proctinhtrang);
                helper.Close();
                foreach (DataRow row in dt.Rows)
                {
                    ListViewItem item = new ListViewItem();
                    item.Text = row[0].ToString();

                    foreach (DataRow rowloai in dtloai.Rows)
                    {
                        if (rowloai[0].ToString() == row[2].ToString())
                        {
                            item.SubItems.Add(rowloai[1].ToString());
                        }
                    }
                    item.SubItems.Add(row[3].ToString());
                    if (row[4].ToString() != "")
                    {
                        item.SubItems.Add(row[4].ToString());
                    }
                    else
                    {
                        item.SubItems.Add("NULL");
                    }


                    item.SubItems.Add(row[5].ToString());
                    item.SubItems.Add(row[6].ToString());
                    if (row[7].ToString() != "")
                    {
                        item.SubItems.Add(row[7].ToString());
                    }
                    else
                    {
                        item.SubItems.Add("0");
                    }

                    foreach (DataRow rowtinhtrang in dttinhtrang.Rows)
                    {
                        if (rowtinhtrang[0].ToString() == row[8].ToString())
                        {
                            item.SubItems.Add(rowtinhtrang[1].ToString());
                        }
                    }
                    listView1.Items.Add(item);

                }
                rdbtndung.Checked = true;
            }
            catch (SqlException ex)
            {
                MessageBox.Show("Lỗi " + ex.Message);
            }
        }
        #region LOAD LAI//
        public void loadlai(string chuoi)
        {
            try
            {
                Datahelper helper = new Datahelper();
                helper.Open();
                string proc = chuoi;
                string procloai = "sp_LayDanhSachLoaiUser";
                string proctinhtrang = "sp_LayDanhSachTinhTrangUser";
                DataTable dt = new DataTable();
                DataTable dtloai = new DataTable();
                DataTable dttinhtrang = new DataTable();
                dt = helper.ExecuteQueryStore(proc);
                dtloai = helper.ExecuteQueryStore(procloai);
                dttinhtrang = helper.ExecuteQueryStore(proctinhtrang);
                helper.Close();
                listView1.Items.Clear();
                foreach (DataRow row in dt.Rows)
                {
                    ListViewItem item = new ListViewItem();
                    item.Text = row[0].ToString();

                    foreach (DataRow rowloai in dtloai.Rows)
                    {
                        if (rowloai[0].ToString() == row[2].ToString())
                        {
                            item.SubItems.Add(rowloai[1].ToString());
                        }
                    }
                    item.SubItems.Add(row[3].ToString());
                    if (row[4].ToString() != "")
                    {
                        item.SubItems.Add(row[4].ToString());
                    }
                    else
                    {
                        item.SubItems.Add("NULL");
                    }


                    item.SubItems.Add(row[5].ToString());
                    item.SubItems.Add(row[6].ToString());
                    if (row[7].ToString() != "")
                    {
                        item.SubItems.Add(row[7].ToString());
                    }
                    else
                    {
                        item.SubItems.Add("0");
                    }

                    foreach (DataRow rowtinhtrang in dttinhtrang.Rows)
                    {
                        if (rowtinhtrang[0].ToString() == row[8].ToString())
                        {
                            item.SubItems.Add(rowtinhtrang[1].ToString());
                        }
                    }
                    listView1.Items.Add(item);

                }
               
            }
            catch (SqlException ex)
            {
                MessageBox.Show("Lỗi " + ex.Message);
            }
        }
        #endregion

        private void btnXemlai_Click(object sender, EventArgs e)
        {
            if (rdbtndung.Checked == true)
                loadlai("sp_LayDanhSachUser");
            else
                loadlai("read_uncommitted_xemdanhsachuser");
        }
    }
}